package com.opi.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;

import com.opi.thread.CpuCollectionThread;
import com.opi.thread.NetworkCollectionThread;
import com.opi.util.CommUtil;
import com.opi.util.ReadScript;
/**
 * 初始化 系统
 * @author 维康
 *
 */
public class InitSystem extends HttpServlet {

	private  Logger logger = Logger.getLogger(InitSystem.class);
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	/**
	 * Initialization of the servlet. <br>
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		String monitor_net = ReadScript.getNetMonitor();
		String monitor_cpu = ReadScript.getCpuMonitor();
		logger.info("初始化系统:优化监控脚本..");
		//增加可执行权限
		String c1 = CommUtil.sendCmd("chmod +x  "+monitor_net);
		//修复win编写的脚本和linux编码格式不统一
		String c2 = CommUtil.sendCmd("sed -i 's/\\\r$//'  "+monitor_net);
		logger.info("增加可执行权限:"+monitor_net+" | "+ c1);
		logger.info("修复脚本编码:"+monitor_net+" | "+ c2);
		
		String c3  = CommUtil.sendCmd("chmod +x "+monitor_cpu );
		String c4 =CommUtil.sendCmd("sed -i 's/\\\r$//' "+monitor_cpu);
		logger.info("增加可执行权限:"+monitor_cpu+" | "+c3);
		logger.info("修复脚本编码:"+monitor_cpu+" | "+ c4);
		
		//创建线程 ， 采样监控数据
		CpuCollectionThread t1 = new CpuCollectionThread();
		NetworkCollectionThread t2 = new NetworkCollectionThread();
		logger.info("开始采样cpu信息...");
		t1.start();
		logger.info("开始采样网络信息...");
		t2.start();
	}

}
